package com.example.shiwu.service;

import android.content.Context;

import com.example.shiwu.db.dao.BoxFourDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.BoxFour;
import com.example.shiwu.db.model.BoxOne;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class BoxFourService {

    private DatabaseHelper dbHelper;
    private BoxFourDao<BoxFour> BoxFourDao;
    private ArrayList<BoxFour> listBoxFour = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public BoxFourService() {
    }

    public BoxFourService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }


    /**获取单个数据
     * @param BoxFour
     * @return
     */
    public List<BoxFour> getBoxFour(BoxFour BoxFour) {
        BoxFourDao = BaseDaoFactory.getInstance().createBaseDao(BoxFourDao.class, BoxFour.class);
        listBoxFour = new ArrayList<>();
        listBoxFour.addAll(BoxFourDao.query(BoxFour));
        return listBoxFour;
    }

    /**分页获取数据
     * @param BoxFour
     * @return
     */
    public List<BoxFour> getBoxFourList(BoxFour BoxFour,Integer startIndex,Integer limit) {
        BoxFourDao = BaseDaoFactory.getInstance().createBaseDao(BoxFourDao.class, BoxFour.class);
        listBoxFour = new ArrayList<>();
        listBoxFour.addAll(BoxFourDao.query(BoxFour,null,"create_time desc",null,startIndex,limit));
        return listBoxFour;
    }


    public boolean save(BoxFour BoxFour) {
        BoxFourDao = BaseDaoFactory.getInstance().createBaseDao(BoxFourDao.class, BoxFour.class);
        long size = BoxFourDao.insert(BoxFour);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(BoxFour BoxFour) {
        BoxFourDao = BaseDaoFactory.getInstance().createBaseDao(BoxFourDao.class, BoxFour.class);
        BoxFour boxFour = new BoxFour();
        boxFour.setItemId(BoxFour.getItemId());
        List<BoxFour> lis =BoxFourDao.query(boxFour);
        Boolean bl = true;
        if(lis.size()>0){
            BoxFour.setUpdateTime(DateUtils.parseDate());
            long size = BoxFourDao.update(BoxFour,new BoxFour());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            BoxFour.setCreateTime(DateUtils.parseDate());
            long size = BoxFourDao.insert(BoxFour);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updade(BoxFour BoxFour,BoxFour where) {
        long size = BoxFourDao.update(BoxFour,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public Boolean delete(BoxFour boxFour) {
        int size = BoxFourDao.delete(boxFour);
        if(size >0 ){
            return true;
        }
        return false;
    }
}
